package cz.possoft.client.dama.gui.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;

import cz.possoft.client.gui.gamepanel.components.GamePane;
import cz.possoft.serializable.dama.figurky.DamaDama;
import cz.possoft.serializable.dama.figurky.DamaFigurka;
import cz.possoft.serializable.dama.figurky.DamaKamen;
import cz.possoft.serializable.dama.figurky.DamaPrazdna;
import cz.possoft.serializable.dama.game.DamaPole;
import cz.possoft.serializable.generic.HraciPole;

/**
 * Vizualni hraci pole pro hru Dama
 * 
 * @author mara
 */
public class DamaPane extends GamePane {
	
	// Obrazky vsech figurek
    private Image blackPrazdna;
    private Image whitePrazdna;
    
    private Image blackKamenBlack;    
    private Image whiteKamenBlack;        
    
    private Image blackDamaBlack;    
    private Image whiteDamaBlack;     
	
    public DamaPane() {    	
    }
    
    // metoda nacte vsecny obrazky do promennych image
    private void loadImages() {
    try {
        blackKamenBlack=new ImageIcon("figurky-dama/blackKamen-black.GIF").getImage();        
        whiteKamenBlack=new ImageIcon("figurky-dama/whiteKamen-black.GIF").getImage();        
        
        blackDamaBlack=new ImageIcon("figurky-dama/blackDama-black.GIF").getImage();        
        whiteDamaBlack=new ImageIcon("figurky-dama/whiteDama-black.GIF").getImage();        
        
        blackPrazdna=new ImageIcon("figurky-dama/blackPole.GIF").getImage();
        whitePrazdna=new ImageIcon("figurky-dama/whitePole.GIF").getImage();
        }
        catch (Exception ex) { System.err.println("Exception pri nacitani image figurek.");
                               ex.printStackTrace(); 
        }
    }    
    
    // nasetuje hraci pole a provede dalsi inicializacni akce,
    // nakresleni atd.
    public void init(HraciPole hPole) {
    	loadImages(); 
    	setHraciPole(hPole);
    }
    
    // nakresli figurku na souradnici x,y. Nakresli se odpovidajici figurka z objektu sachovnice
    private void paintFigurka(int x,int y,Graphics g) {        
        boolean cerna;
        if ((x+y)%2==1) cerna=true;
        else cerna=false;
        
        DamaFigurka fig = ((DamaPole)getHraciPole()).getFigurky()[x][y];
        
        if ((fig instanceof DamaPrazdna) && (cerna)) g.drawImage(blackPrazdna,4+x*55,4+y*55,this);
        if ((fig instanceof DamaPrazdna) && !(cerna)) g.drawImage(whitePrazdna,4+x*55,4+y*55,this);
        
        // KRESLENI CERNYCH FIGUREK
        if (fig.getColor()==1) {
        
            if (fig instanceof DamaKamen) g.drawImage(blackKamenBlack,4+x*55,4+y*55,this);                        
            if (fig instanceof DamaDama) g.drawImage(blackDamaBlack,4+x*55,4+y*55,this);            
            
        }
        if  (fig.getColor()==2) { // KRESLENI BILYCH FIGUREK
            if (fig instanceof DamaKamen) g.drawImage(whiteKamenBlack,4+x*55,4+y*55,this);                        
            if (fig instanceof DamaDama) g.drawImage(whiteDamaBlack,4+x*55,4+y*55,this);            
        
        }
        if (fig.isRing()) {
            g.setColor(Color.YELLOW);
            g.drawRect(4 + x*55, 4 + y*55, 54, 54);
        }
        if (fig.isMarkedForPlay()) {
            g.setColor(Color.BLUE);
            g.drawRect(4 + x*55, 4 + y*55, 54, 54);
        }
        
        }    
    
    // Nakresleni sachovnice     
    public void paint(Graphics g)  {
     super.paint(g);
     g.setColor(Color.BLUE);
     g.fillRect(0,0,450,450);
     
     for (int i=0;i<8;i++)
         for (int j=0;j<8;j++) {
         paintFigurka(i,j,g);
         }
   }                

}
